package com.ym.controller.management;

import com.github.pagehelper.PageInfo;

import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.exception.BusinessException;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.excel.ExportExcelUtil;
import com.ym.core.utils.excel.ImportExcelUtil;

import com.ym.medical.domain.entity.CertificateImageEntity;
import com.ym.medical.domain.entity.DoctorDetailsEntity;
import com.ym.medical.domain.entity.HospitalDeptEntity;
import com.ym.medical.domain.entity.LabelsEntity;
import com.ym.medical.domain.param.DoctorDetailsParam;
import com.ym.medical.domain.param.LabelsParam;
import com.ym.medical.domain.vo.DoctorAddVo;
import com.ym.medical.domain.vo.DoctorDetailListVo;
import com.ym.medical.domain.vo.DoctorDetailsVo;
import com.ym.medical.domain.vo.LabelsVo;
import com.ym.medical.service.ICertificateImageSV;
import com.ym.medical.service.IDoctorDetailSV;
import com.ym.medical.service.IHospitalDeptSV;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 综合管理中心
 */
@Api(tags = "医生医生详情")
@Controller
@RequestMapping("/management/doctor")
public class DoctorDetailController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(DoctorDetailController.class);

	@Autowired
	private IDoctorDetailSV iDoctorDetailSV;
	@Autowired
	private IHospitalDeptSV iHospitalDeptSV;
	@Autowired
	private ICertificateImageSV iCertificateImageSV;


	@ApiOperation("医生列表")
	@GetMapping("/getDoctorDetailList")
	@ResponseBody
	public APIResponse<PageInfo<DoctorDetailListVo>> getDoctorDetailList(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "10")
					int limit,
			@ApiParam(name = "doctorDetailsModel", value = "医生信息", required = false)
					DoctorDetailsParam doctorDetailsModel) throws Exception {
		PageInfo<DoctorDetailListVo> doctorDetailsPageInfo =
				iDoctorDetailSV.getDoctorDetailList(pageNo, limit, doctorDetailsModel);
		return successData(doctorDetailsPageInfo);
	}

	@ApiOperation("医生信息导出")
	@GetMapping("/exportDoctor")
	public void exportDoctor(
			@ApiParam(name = "pageNo", value = "页码", required = true)
			@RequestParam(name = "pageNo", required = true, defaultValue = "1")
					int pageNo,
			@ApiParam(name = "limit", value = "每页数量", required = false)
			@RequestParam(name = "limit", required = false, defaultValue = "1000000")
					int limit,
			@ApiParam(name = "doctorDetailsModel", value = "医生信息", required = false)
					DoctorDetailsParam doctorDetailsModel,
			HttpServletResponse response) throws Exception {
		ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
		String titleName = "医生信息表";
		String[] headerString = {"医院名称", "科室名称", "医生编码", "医生姓名", "医生性别", "认证状态", "认证时间", "手机号", "证件类型", "证件号码"};
		String[] headerField = {"hosName", "deptName", "doctCode", "realName", "sexName", "qualificationStatusName", "qualificationTime", "phoneNo", "cardTypeName", "cardNo"};
		PageInfo<DoctorDetailsEntity> doctorPageinfo = iDoctorDetailSV.getAllDoctorList(pageNo, limit, doctorDetailsModel);
		exportExcelUtil.exportExcelForTotal(titleName, headerString, headerField, null, doctorPageinfo.getList(), response, null);

	}

	@ApiOperation("医生信息导入")
	@PostMapping("/importDoctor")
	@ResponseBody
	public APIResponse<Map<String, Object>> importDoctor(
			@ApiParam(name = "excelFile", value = "文件名", required = true)
			@RequestBody MultipartFile excelFile) throws Exception {
		if (excelFile.isEmpty()) {
			return failMsg("文件不存在");
		}
		Map<String, Object> map = new HashMap<>();
		String fileName = excelFile.getOriginalFilename();
		InputStream inputStream = null;
		int startLine = 2;
		try {
			inputStream = excelFile.getInputStream();
			List<DoctorDetailsParam> doctorDetailsList =
					ImportExcelUtil.readExcel(
							inputStream, fileName, startLine, DoctorDetailsParam.class, ImportExcelUtil.getDoctorTemplate());
			if (doctorDetailsList.isEmpty()) {
				throw new BusinessException("您的excel没有有效的数据 请添加数据后再试!");
			}
			map = iDoctorDetailSV.importDoctor(doctorDetailsList);

		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw new BusinessException(e.getMessage());
			}
			logger.info("importDoctor", e.getMessage());
		} finally {
			if (inputStream != null) {
				inputStream.close();
			}
		}
		return successData(map);
	}

	@ApiOperation("医生信息删除")
	@PostMapping("/deleteDoctor")
	@ResponseBody
	public APIResponse deleteDoctor(
			@ApiParam(name = "doctorIds", value = "医生ids", required = true) @RequestBody Long[] doctorIds) throws Exception {
		int rows = iDoctorDetailSV.deleteDoctor(doctorIds);
		if (rows == 0) {
			return failMsg("删除失败");
		}
		return successMsg("删除成功");
	}

	@ApiOperation("医生信息新增")
	@PostMapping("/addDoctor")
	@ResponseBody
	public APIResponse<DoctorAddVo> addDoctor(
			@ApiParam(name = "addVo", value = "医生信息", required = true)
			@RequestBody DoctorAddVo addVo
	) {
		Long currId = ShiroUtil.getLoginId();
		int rows = iDoctorDetailSV.addDoctor(addVo, currId);
		if (rows == 0) {
			return failMsg("新增失败");
		}
		return successMsg("新增成功");
	}

	@ApiOperation("医生信息更改")
	@PostMapping("/updateDoctor")
	@ResponseBody
	public APIResponse<DoctorAddVo> updateDoctor(
			@ApiParam(name = "updateVo", value = "医生信息", required = true)
			@RequestBody DoctorAddVo updateVo) throws Exception {
		Long currId = ShiroUtil.getLoginId();
		int rows = iDoctorDetailSV.updateDoctor(updateVo, currId);
		if (rows == 0) {
			return failMsg("更改医生信息失败");
		}
		return successMsg("更改成功");
	}

	@ApiOperation("医院code 查询对应医院所有科室")
	@GetMapping("/selectDept")
	@ResponseBody
	public APIResponse selectDepts(
			@ApiParam(name = "hosCode", value = "医院code", required = true)
					String hosCode) throws Exception {
		List<HospitalDeptEntity> deptNames = iHospitalDeptSV.selectDept(hosCode);
		if (null == deptNames) {
			return failMsg("请求失败");
		}
		return successData(deptNames);
	}

	@ApiOperation("医生标签新增")
	@PostMapping("/addJobTitle")
	@ResponseBody
	public APIResponse addJobTitle(
			@ApiParam(name = "doctId", value = "医生id", required = true)
					Long doctId,
			@ApiParam(name = "label", value = "标签 ", required = true)
			@Validated(value = {LabelsEntity.Add.class}) String label) throws Exception {
		Long currId = ShiroUtil.getLoginId();
		int rows = iDoctorDetailSV.addJobTitle(doctId, label, currId);
		if (rows != 1) {
			return failMsg("添加失败");
		}
		return successMsg("添加成功");
	}

	@ApiOperation("医生标签删除")
	@GetMapping("/deleteJobTitle")
	@ResponseBody
	public APIResponse deleteJobTitle(
			@ApiParam(name = "labId", value = "标签id", required = true)
					Long labId) throws Exception {
		int rows = iDoctorDetailSV.deleteJobTitle(labId);
		if (rows != 1) {
			return failMsg("删除失败");
		}
		return successMsg("删除成功");
	}

	@ApiOperation("医生标签查询")
	@GetMapping("/selectJobTitle")
	@ResponseBody
	public APIResponse selectJobTitle(
			@ApiParam(name = "doctId", value = "医生id", required = true)
					Long doctId) throws Exception {
		List<LabelsEntity> labelsList = iDoctorDetailSV.selectJobTitle(doctId);
		if (labelsList.isEmpty()) {
			return success();
		}
		return successData(labelsList);
	}

	@Log
	@ApiOperation("医生认证图片")
	@GetMapping("/certImageInfo")
	@ResponseBody
	public APIResponse certInfo(
			@ApiParam(name = "doctId", value = "医生id", required = true)
			@RequestParam Long doctId) throws Exception {
		List<CertificateImageEntity> certificateImages = iCertificateImageSV.selectCertImage(doctId);
		return successData(certificateImages);
	}


	@Log
	@ApiOperation("医生编码重复校验")
	@GetMapping("/repeat/DoctCode")
	@ResponseBody
	public APIResponse repeatDoctCode(
			@ApiParam(name = "doctCode", value = "医生编码", required = true)
			@RequestParam String doctCode,
			@ApiParam(name = "hosCode", value = "医院编码", required = true)
			@RequestParam String hosCode) {
		iDoctorDetailSV.repeatDoctCode(doctCode, hosCode);
		return success();

	}

	@Log
	@ApiOperation("医生所有分类擅长列表")
	@PostMapping("/doctor/goods")
	@ResponseBody
	public APIResponse<List<LabelsVo>> doctorGoogs(
			@ApiParam(name = "labelsModel", value = "model 对象", required = false)
			@RequestBody LabelsParam labelsModel) {
		List<LabelsVo> doctGoods = iDoctorDetailSV.selectGoods(labelsModel);
		return successData(doctGoods);
	}


}
